﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCCourseNewest : System.Web.UI.UserControl
	{
		private ICourseProvider _CourseProvider;
		//private List<TabIndex> _lstTabs;

		protected void Page_Load(object sender, EventArgs e)
		{
			BindCourseCategory();
			//BindData();
		}

		protected void rptCourseCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var dataItem = e.Item.DataItem as CourseCategory;
				var hlCourseCategory = e.Item.FindControl("hlCourseCategory") as HyperLink;

				hlCourseCategory.Text = dataItem.CourseCateogryName;
				//hlCourseCategory.NavigateUrl = "#tab" + dataItem.CourseCateogryID.ToString();
				hlCourseCategory.Attributes.Add("href", "#tab" + dataItem.CourseCateogryID.ToString());
			}
		}

		protected void rptCourseContainer_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var dataItem = e.Item.DataItem as TabIndex;

				var courseCategoryID = dataItem.Index;
				var rptCourse = e.Item.FindControl("rptCourse") as Repeater;

				if(_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				var user = SiteContext.Current.CurrentUser;

				if(user != null)
				{
					if(user.Role != null && user.Role.RoleName.Equals("admin"))
					{
						var drugStore = SiteContext.Current.CurrentDrugStore;

						if(drugStore != null)
						{
							rptCourse.DataSource = _CourseProvider.GetNewCourseByCategory(courseCategoryID, drugStore.DrugStoreID, 6);
							rptCourse.DataBind();
						}
					}
					else if(user.DrugStoreID.GetValueOrDefault(0) > 0)
					{
						rptCourse.DataSource = _CourseProvider.GetNewCourseByCategory(user.UserID, courseCategoryID, user.DrugStoreID.GetValueOrDefault(0), 6);
						rptCourse.DataBind();
					}
				}
			}
		}

		protected void rptCourse_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var dataItem = e.Item.DataItem as Course;
				var lblCourseName = e.Item.FindControl("lblCourseName") as Label;
				var lblCoursePrice = e.Item.FindControl("lblCoursePrice") as Label;

				if(dataItem.CourseName.Length > 14)
				{
					lblCourseName.Text = dataItem.CourseName.Substring(0, 14) + "...";
				}
				else
				{
					lblCourseName.Text = dataItem.CourseName;
				}
				lblCourseName.ToolTip = dataItem.CourseName;

				var price = dataItem.CourseChapters.Sum(cc => cc.CourseChapterReqiredCredits);

				lblCoursePrice.Text = price.GetValueOrDefault(0).ToString() + "(积分)";
				;
			}
		}

		private void BindCourseCategory()
		{
			if(_CourseProvider == null)
			{
				_CourseProvider = new CourseProvider();
			}

			var courseCategory = _CourseProvider.GetAllCourseCategory();

			var lstTabs = new List<TabIndex>();
			foreach(var cc in courseCategory)
			{
				lstTabs.Add(new TabIndex(cc.CourseCateogryID));
			}

			rptCourseCategory.DataSource = courseCategory;
			rptCourseCategory.DataBind();

			rptCourseContainer.DataSource = lstTabs;
			rptCourseContainer.DataBind();
		}

		//private void BindData()
		//{
		//  if (_CourseProvider == null)
		//  {
		//    _CourseProvider = new CourseProvider();
		//  }

		//  var user = SiteContext.Current.CurrentUser;

		//  if (user != null)
		//  {
		//    if (user.Role != null && user.Role.RoleName.Equals("admin"))
		//    {
		//      var drugStore = SiteContext.Current.CurrentDrugStore;

		//      if (drugStore != null)
		//      {
		//        rptCourse.DataSource = _CourseProvider.GetNewCourse(8, drugStore.DrugStoreID);
		//        rptCourse.DataBind();
		//      }
		//    }
		//    else if (user.DrugStoreID.GetValueOrDefault(0) > 0)
		//    {
		//      rptCourse.DataSource = _CourseProvider.GetNewCourse(8, user.DrugStoreID.Value, user.UserID);
		//      rptCourse.DataBind();
		//    }
		//  }
		//}
	}
}